package site.huoqiang.emos.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.huoqiang.emos.common.util.PageUtils;
import site.huoqiang.emos.exception.EmosException;
import site.huoqiang.emos.mapper.MeetingRoomMapper;
import site.huoqiang.emos.pojo.MeetingRoom;
import site.huoqiang.emos.service.MeetingRoomService;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author: huoqiang huoqiang
 * @date: 2022-03-03
 */
@Service
@Slf4j
public class MeetingRoomServiceImpl implements MeetingRoomService {

    @Autowired
    private MeetingRoomMapper meetingRoomMapper;

    /**
     * 查询所有会议室
     *
     * @return
     */
    @Override
    public ArrayList<HashMap> searchAllMeetingRoom() {
        return meetingRoomMapper.searchAllMeetingRoom();
    }

    /**
     * 根据ID查找会议室
     *
     * @param id id
     * @return
     */
    @Override
    public HashMap searchById(int id) {
        return meetingRoomMapper.searchById(id);
    }

    /**
     * 查询空闲会议室
     *
     * @param param
     * @return
     */
    @Override
    public ArrayList<String> searchFreeMeetingRoom(HashMap param) {
        return meetingRoomMapper.searchFreeMeetingRoom(param);
    }

    /**
     * 查询线下会议室分页数据
     *
     * @param param
     * @return
     */
    @Override
    public PageUtils searchMeetingRoomByPage(HashMap param) {
        ArrayList<HashMap> list = meetingRoomMapper.searchMeetingRoomByPage(param);
        Long count = meetingRoomMapper.searchMeetingRoomCount(param);
        int start = (Integer) param.get("start");
        int length = (Integer) param.get("length");
        return new PageUtils(list, count, start, length);
    }

    /**
     * 添加会议室
     *
     * @param meetingRoom
     * @return
     */
    @Override
    public int insert(MeetingRoom meetingRoom) {
        return meetingRoomMapper.insert(meetingRoom);
    }

    /**
     * 修改会议室
     *
     * @param meetingRoom
     * @return
     */
    @Override
    public int update(MeetingRoom meetingRoom) {
        return meetingRoomMapper.update(meetingRoom);
    }

    /**
     * 删除会议
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteMeetingRoomByIds(Integer[] ids) {
        if (!meetingRoomMapper.searchCanDelete(ids)) {
            throw new EmosException("无法删除关联会议的会议室");
        }
        return  meetingRoomMapper.deleteMeetingRoomByIds(ids);
    }
}
